package com.fangyahao.role.base;

import com.alibaba.fastjson.JSON;

import javax.servlet.GenericServlet;
import javax.servlet.ServletResponse;
import java.io.PrintWriter;
import java.util.Map;

public abstract  class BaseController extends GenericServlet {

    protected final static String GET_METHOD ="GET";
    protected final static String POST_METHOD ="POST";

    protected void printData(boolean check,Object data,Integer code,ServletResponse response){
        String json="";
        if(check){
            Map<String,Object> map = BaseBean.success(data);
            json = JSON.toJSONString(map);
        }else{
            if(code== 500){
                json =JSON.toJSONString(BaseBean.failure(data));
            }else if(code== 404){
                json =JSON.toJSONString(BaseBean.notFound(data));
            }
        }

        response.setContentType("text/html; charset=UTF-8");
        try (PrintWriter writer = response.getWriter()) {
            writer.print(json);
            writer.flush();
        }catch (Exception exception){
            System.out.println(exception);
        }
    }
}
